﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;

#if CONSOLEMODE
using FRL.DebtChased.Repository;
using System.Configuration;
using System.IO;
#endif // #if CONSOLEMODE

namespace FRL.DebtChased.ClockworkService
{

	/// <summary>
	/// Declaration of Program class
	/// </summary>
	static class Program
	{

		/////////////////////////////////////////////////////////////
		// Operations

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		static void Main()
		{

			// Compare console modes
#if CONSOLEMODE

				// Create service object
				ClockworkService svc = new ClockworkService();

				// Dump commands
				_showHelp();

				// Loop through commands
				while (true)
				{

					// Get the next command string
					string strCmd = Console.ReadLine();
					if (string.Compare(strCmd, "send sms", true) == 0)
						_sendTestSMS();
					else if (string.Compare(strCmd, "send email", true) == 0)
						_sendTestEmail();
					else if (string.Compare(strCmd, "start all", true) == 0)
						svc.StartAllThreads();
					else if (string.Compare(strCmd, "stop all", true) == 0)
						svc.StopAllThreads();
					else if (string.Compare(strCmd, "start newacc", true) == 0)
						svc.StartNewAccountThread(true);
					else if (string.Compare(strCmd, "stop newacc", false) == 0)
						svc.StartNewAccountThread(false);
					else if (string.Compare(strCmd, "start wf", true) == 0)
						svc.StartWorkflowThread(true);
					else if (string.Compare(strCmd, "stop wf", false) == 0)
						svc.StartWorkflowThread(false);
					else if (string.Compare(strCmd, "start msg", true) == 0)
						svc.StartMessagingThread(true);
					else if (string.Compare(strCmd, "stop msg", false) == 0)
						svc.StartMessagingThread(false);
					else if (string.Compare(strCmd, "test word", false) == 0)
						_testWordSession();
					else if (string.Compare(strCmd, "test pdf", false) == 0)
						_testWordPdfSession();
					else if (string.Compare(strCmd, "help", false) == 0)
						_showHelp();
					else if (string.Compare(strCmd, "quit", true) == 0)
						break;
					else if (strCmd.Length == 0)
					{
					}
					else
						Console.WriteLine("Unknown command \"{0}\"", strCmd);

				} // while-loop

				// Make sure the workers have stopped
				svc.StopAllThreads();

				// End of console application
				Console.WriteLine("Press any key to exit...");
				Console.ReadKey();

#else

				// Load array of service objects
				ServiceBase[] ServicesToRun;
				ServicesToRun = new ServiceBase[] 
				{ 
					new ClockworkService() 
				};

				// Run the service
				ServiceBase.Run(ServicesToRun);

#endif // CONSOLEMODE

		}

#if CONSOLEMODE
		private static void _showHelp()
		{

			// Dump commands
			Console.WriteLine("Commands:");
			Console.WriteLine("   help - show this information");
			Console.WriteLine("   send sms - send a test sms message to Simon");
			Console.WriteLine("   send email - send a test email to Simon");
			Console.WriteLine("   start all - start all worker threads");
			Console.WriteLine("   stop all - stop all worker threads");
			Console.WriteLine("   quit - stop application");
			Console.WriteLine("   start newacc - start the new accounts thread");
			Console.WriteLine("   stop newacc - stop the new account thread");
			Console.WriteLine("   start wf - start the workflow thread");
			Console.WriteLine("   stop wf - stop the workflow thread");
			Console.WriteLine("   start msg - start the messaging thread");
			Console.WriteLine("   stop msg - stop the messaging thread");
			Console.WriteLine("   test word - create a Word doc and stuff");
			Console.WriteLine("   test pdf - create a PDF from a Word doc");

		}
#endif // CONSOLEMODE

#if CONSOLEMODE
		private static void _sendTestSMS()
		{

			// Call messaging function
            //string strError = Messaging.SendSMS("020 7100 9317", "Test message");
            string strError = Messaging.SendSMS("07779258711", "Test message");
			if(strError==null)
				Console.WriteLine("SMS sent OK");
			else
				Console.WriteLine(strError);

		}
#endif // #if CONSOLEMODE

#if CONSOLEMODE
		private static void _sendTestEmail()
		{

			// Call messaging function
			string strError = Messaging.SendEmail("simon@computerc.com", "Test subject", "Test message");
			if (strError == null)
				Console.WriteLine("Email sent OK");
			else
				Console.WriteLine(strError);

		}
#endif // #if CONSOLEMODE

#if CONSOLEMODE
		private static void _testWordSession()
		{

			// Get the template folder-path
			string strTemplateFolderPath = ConfigurationManager.AppSettings["TemplateRootFolderPath"];
			if (string.IsNullOrEmpty(strTemplateFolderPath))
			{
				Console.WriteLine("TemplateRootFolderPath setting not found in application config.");
				return;
			}
			if(!Directory.Exists(strTemplateFolderPath))
			{
				Console.WriteLine("TemplateRootFolderPath {0} not found.", strTemplateFolderPath);
				return;
			}

			// Get a client-docs folder-path
			string strClientDocsFolderPath = ConfigurationManager.AppSettings["ClientDocsRootFolderPath"];
			if (string.IsNullOrEmpty(strClientDocsFolderPath))
			{
				Console.WriteLine("ClientDocsRootFolderPath setting not found in application config.");
				return;
			}
			if (!Directory.Exists(strClientDocsFolderPath))
			{
				Console.WriteLine("ClientDocsRootFolderPath {0} not found.", strClientDocsFolderPath);
				return;
			}

			// Call test function
			List<string> Results = WordSession.RunDebugTest(strTemplateFolderPath, strClientDocsFolderPath);
			ConsoleColor oldColor = Console.ForegroundColor;
			Console.ForegroundColor = ConsoleColor.Yellow;
			foreach (string strLine in Results)
				Console.WriteLine(strLine);
			Console.ForegroundColor = oldColor;

		}
#endif // #if CONSOLEMODE

#if CONSOLEMODE
		private static void _testWordPdfSession()
		{

			// Get a client-docs folder-path
			string strClientDocsFolderPath = ConfigurationManager.AppSettings["ClientDocsRootFolderPath"];
			if (string.IsNullOrEmpty(strClientDocsFolderPath))
			{
				Console.WriteLine("ClientDocsRootFolderPath setting not found in application config.");
				return;
			}
			if (!Directory.Exists(strClientDocsFolderPath))
			{
				Console.WriteLine("ClientDocsRootFolderPath {0} not found.", strClientDocsFolderPath);
				return;
			}

			// Compose filepath
			string strFilepath = Path.Combine(strClientDocsFolderPath, "test1.docx");
			if(!File.Exists(strFilepath))
			{
				Console.WriteLine("Test Word file {0} not found.", strFilepath);
				return;
			}

			// Convert file
			string strTargetFilepath = Path.Combine(strClientDocsFolderPath, "test1.pdf");
			string strError = WordSession.ConvertToPdf(strFilepath, strTargetFilepath);
			if(strError!=null)
				Console.WriteLine(strError);
			else
				Console.WriteLine("OK");

		}
#endif // #if CONSOLEMODE

	}

}
